package com.foryou.wechat.service;

import com.foryou.wechat.common.enums.ChargingModelEnum;
import com.foryou.wechat.common.enums.ShootTypeEnum;
import com.foryou.wechat.entity.PlaceEntity;
import com.foryou.wechat.entity.ProductionEntity;
import com.foryou.wechat.entity.ShootEntity;
import com.foryou.wechat.entity.UserEntity;
import com.foryou.wechat.mapper.PlaceMapper;
import com.foryou.wechat.mapper.ProductionMapper;
import com.foryou.wechat.mapper.ShootMapper;
import com.foryou.wechat.mapper.UserMapper;
import com.foryou.wechat.model.wx.TemplateParam;
import com.foryou.wechat.utils.EnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WXHandlerService {

    @Value("${wx.templateId}")
    private String templateId;

    private final WeChatService weChatService;

    private final UserMapper userMapper;

    private final ProductionMapper productionMapper;

    private final ShootMapper shootMapper;

    private final PlaceMapper placeMapper;

    public WXHandlerService(PlaceMapper placeMapper, WeChatService weChatService, UserMapper userMapper, ProductionMapper productionMapper, ShootMapper shootMapper) {
        this.placeMapper = placeMapper;
        this.weChatService = weChatService;
        this.userMapper = userMapper;
        this.productionMapper = productionMapper;
        this.shootMapper = shootMapper;
    }

    public void pushUserShootHandler(Long businessId) {
        ShootEntity shootEntity = shootMapper.selectById(businessId);
        handlerPush(shootEntity.getUserId(), getShootContent(shootEntity), shootEntity.getAuditStatus());
    }

    public void pushUserPlaceHandler(Long businessId) {
        PlaceEntity placeEntity = placeMapper.selectById(businessId);
        handlerPush(placeEntity.getUserId(), placeEntity.getPlaceName(), placeEntity.getAuditStatus());
    }

    public void pushUserWorksHandler(Long businessId) {
        ProductionEntity productionEntity = productionMapper.selectById(businessId);
        handlerPush(productionEntity.getUserId(), productionEntity.getName(), productionEntity.getAuditStatus());
    }

    private void handlerPush(Long userId, String content, int auditStatus) {
        UserEntity userEntity = userMapper.selectById(userId);
        List<TemplateParam> templateParamList = new ArrayList<>();

        if (auditStatus != 1) {
            //审核不通过
            templateParamList.add(new TemplateParam("phrase1", "审核不通过"));
            templateParamList.add(new TemplateParam("thing2", "您发布的内容不合规哦~"));

        }else {
            //审核通过
            templateParamList.add(new TemplateParam("phrase1", "审核通过"));
            templateParamList.add(new TemplateParam("thing2", content.concat("审核通过，可进入小程序内查看")));
        }
        templateParamList.add(new TemplateParam("phrase3","个人认证"));
        weChatService.pushOneUser(userEntity.getOpenId(), templateId, templateParamList);
    }

    private String getShootContent(ShootEntity shootEntity) {
        StringBuilder sb = new StringBuilder();
        sb.append(EnumUtil.getEnumByCode(ShootTypeEnum.class, shootEntity.getType()));
        sb.append("-");
        sb.append(EnumUtil.getEnumByCode(ChargingModelEnum.class, shootEntity.getChargingModel()));
        if (shootEntity.getChargingModel() == ChargingModelEnum.CHARGES_NEED.getCode() ||
                shootEntity.getChargingModel() == ChargingModelEnum.WILLING_TO_PAY.getCode()) {
            sb.append("，");
            sb.append(shootEntity.getPrice());
        }
        return sb.toString();
    }

}
